//package com.cookor.poi.modules.phy.schedule;
//
//import com.cookor.poi.modules.phy.entity.TAction;
//import com.cookor.poi.modules.phy.entity.TOrder;
//import com.cookor.poi.modules.phy.entity.TTempNotice;
//import com.cookor.poi.modules.phy.service.*;
//import com.cookor.poi.modules.weixin.account.PlatformAccount;
//import com.cookor.poi.modules.weixin.entity.TWeixinConfig;
//import com.cookor.poi.modules.weixin.open.WxOpenUtil;
//import com.cookor.poi.modules.weixin.service.TWeixinConfigService;
//import com.cookor.poi.modules.weixin.utils.TempNoticeUtil;
//import com.xiaoleilu.hutool.json.JSONObject;
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
///**
// * @Description: TODO
// * @author: scott
// * @date: 2021年05月06日 10:55
// */
//@Service
//@Lazy(false)
//public class ActionSchedule {
//
//    @Autowired
//    private TActionService tActionService;
//
//    @Autowired
//    private TOrderService tOrderService;
//    @Autowired
//    private TBookNoService tBookNoService;
//    @Autowired
//    private TStockService tStockService;
//    @Autowired
//    private TTempNoticeService tTempNoticeService;
//    @Autowired
//    private TWeixinConfigService tWeixinConfigService;
//
//    private final static Logger logger = Logger.getLogger(ActionSchedule.class);
//
//    @Scheduled(cron = "0 */2 * * * ?") //每2分钟执行一次
//    /** 活动结束定时器*/
//    @Transactional(readOnly = false, rollbackFor = Exception.class)
//    public void checkActionStatus()throws Exception{
//        logger.info("=============活动结束定时器启动=============");
//
//        // 查询出所有未开始的活动
//        TAction tAction = new TAction();
//        tAction.setStatus(TAction.action_status_wait);
//        List<TAction> list = tActionService.findList(tAction);
//
//        List<String> actionIds = new ArrayList<>();
//        //获取当前时间
//        Date currentDate = new Date();
//        for(TAction action : list){
//            // 开始时间大于当前时间，记录id 进行中
//            if(action.getBeginDate().getTime() < currentDate.getTime())
//                actionIds.add(action.getId());
//        }
//        // 批量更新活动状态为进行中
//        if(!actionIds.isEmpty()) {
//            tActionService.actionStatusByIds(actionIds, TAction.action_status_ing);
//        }
//        // ---------------------上面进行中，下面结束------------------------------
//        // 查询所有进行中的活动
//        tAction = new TAction();
//        tAction.setStatus(TAction.action_status_ing);
//        List<TAction> ingList = tActionService.findList(tAction);
//
//        List<TAction> actionOrderList = new ArrayList<>();
//        actionIds = new ArrayList<>();
//        for(TAction action : ingList){
//            // 如果结束时间小于当前时间 记录id 已结束
//            if(action.getEndDate().getTime() < currentDate.getTime()) {
//                actionIds.add(action.getId());
//                actionOrderList.add(action);
//            }
//        }
//        // 更新
//        TOrder tOrder;
//        List<TOrder> orderList;
//        List<Integer> successActionId = new ArrayList<>();
//        List<Integer> failActionId = new ArrayList<>();
//        TTempNotice tempNotice;
//        TWeixinConfig wc;
//        if(!actionIds.isEmpty()) {
//            tActionService.actionStatusByIds(actionIds, TAction.action_status_end);
//            for(TAction action : actionOrderList){
//                tOrder = new TOrder();
//                tOrder.setActionId(Integer.valueOf(action.getId()));
//                tOrder.setWithOutCancel(1);
//                orderList = tOrderService.findList(tOrder);
//                if(orderList.isEmpty()){
//                    continue;
//                }
//                if(orderList.size() < action.getMinPeople()){ // 未达到最低人数
//                    tOrderService.updateStatusByOrderList(orderList, TOrder.order_status_cancel, "拼团人数未达到最低限制");
//                    // 回复预约
//                    tBookNoService.updateStatusByOrderList(orderList, 1);
//                    // 回复库存
//                    tStockService.addStockByOrderList(orderList, 1);
//                    failActionId.add(Integer.parseInt(action.getId()));
//                    // 发送模板消息
//                    for (TOrder order : orderList) {
//                        wc = tWeixinConfigService.findByTenantId(Integer.parseInt(order.getProRankName()));
//                        tempNotice = tTempNoticeService.getByParam(Integer.parseInt(order.getProRankName()), TTempNotice.retailFail);
//                        if(tempNotice != null && tempNotice.getStatus() == 1) {
//                            order.setTenantId(Integer.parseInt(order.getProRankName()));
//                            String data = TempNoticeUtil.getAppointFail(tempNotice.getTempId(), order);
//                            PlatformAccount account = new PlatformAccount(wc);
//                            String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + account.getAccessToken();
//                            JSONObject jsonObject = WxOpenUtil.httpPost(url, data);
//                            logger.info("jo:" + jsonObject.toString());
//                        }
//                    }
//                }else{
//                    tOrderService.updateStatusByOrderList(orderList, TOrder.order_status_payed, null);
//                    successActionId.add(Integer.parseInt(action.getId()));
//                    for (TOrder order : orderList) {
//                        wc = tWeixinConfigService.findByTenantId(Integer.parseInt(order.getProRankName()));
//                        tempNotice = tTempNoticeService.getByParam(Integer.parseInt(order.getProRankName()), TTempNotice.appointSuccess);
//                        if(tempNotice != null && tempNotice.getStatus() == 1) {
//                            order.setTenantId(Integer.parseInt(order.getProRankName()));
//                            String data = TempNoticeUtil.getAppointSuccess(tempNotice.getTempId(), order);
//                            PlatformAccount account = new PlatformAccount(wc);
//                            String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + account.getAccessToken();
//                            JSONObject jsonObject = WxOpenUtil.httpPost(url, data);
//                            logger.info("jo:" + jsonObject.toString());
//                        }
//                    }
//                }
//            }
//        }
//        if(!successActionId.isEmpty()){
//            tActionService.actionGroupStatusByIds(successActionId, 1);
//        }
//        if(!failActionId.isEmpty()){
//            tActionService.actionGroupStatusByIds(failActionId, 0);
//        }
//        logger.info("=============活动结束定时器定时器结束=============");
//    }
//
//}
